package geppetto.phraseTable.phrase.feature.global;

import java.util.ArrayList;

import geppetto.phraseHMM.phraseExtraction.extractedphrase.data.ScoreData;
import geppetto.phraseTable.PhraseTable;
import geppetto.phraseTable.phrase.Phrase;
import geppetto.phraseTable.phrase.feature.ProbabilityFeature;
import geppetto.phraseTable.resource.SourceTargetIndexResource;
import gnu.trove.TIntDoubleHashMap;
import gnu.trove.TIntObjectHashMap;

public class ProbabilityGlobalFeatureCalc implements AbstractGlobalFeatureCalc{

	SourceTargetIndexResource phraseCountResource;
	protected final static String SCORE_HEADER = "score";
	protected final static String FEATURE = "prob";
	protected final static String FEATURE_R = "rprob";
	protected boolean shouldPrint = true;
	protected boolean reverse = false;
	protected int order;
	
	public ProbabilityGlobalFeatureCalc(int i, SourceTargetIndexResource phraseCountResource) {
		super();
		this.phraseCountResource = phraseCountResource;
		this.order = i;
	}

	public ProbabilityGlobalFeatureCalc(int i,
			SourceTargetIndexResource phraseCountResource, boolean reverse,boolean shouldPrint) {
		super();
		this.phraseCountResource = phraseCountResource;
		this.shouldPrint = shouldPrint;
		this.order = i;
		this.reverse = reverse;
	}

	@Override
	public void calculateFeature(Phrase phrase, PhraseTable table) {
		if(reverse){
			double totalTarScore = SourceTargetIndexResource.getPhrasesForSize(phraseCountResource.get_targetPhraseSums(), phrase.getSourceLen(), phrase.getTargetLen()).get(phrase._targetPhraseID);
			double score = ((ScoreData)phrase.getData(SCORE_HEADER)).getScore();
			phrase._features.addFeature(FEATURE_R, new ProbabilityFeature(score/totalTarScore),order, shouldPrint);
		}
		else{
			double totalSrcScore = SourceTargetIndexResource.getPhrasesForSize(phraseCountResource.get_sourcePhraseSums(), phrase.getSourceLen(), phrase.getTargetLen()).get(phrase._sourcePhraseID); 
			double score = ((ScoreData)phrase.getData(SCORE_HEADER)).getScore();
			phrase._features.addFeature(FEATURE,new ProbabilityFeature(score/totalSrcScore), order, shouldPrint);
		}
	}

}
